package fun.coding.leetcode;

import java.util.Stack;

public class ValidParentheses {

	public static void main(String[] args) {
		ValidParentheses ins = new ValidParentheses();
		System.out.println(ins.isValid("()"));
	}
	
	public boolean isValid(String s) {
		if (s == null) return false;
		
		if (s.isEmpty()) return true;
		
		Stack<Character> stack = new Stack<Character>();
		
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			
			if (c == '(' || c == '{' || c == '[') {
				stack.push(c);
			}
			else {
				if (stack.isEmpty()) return false;
				
				char top = stack.pop();
				
				switch (top) { 
					case '(':
						if (c != ')') return false;
						break;
					case '[':
						if (c != ']') return false;
						break;
					case '{':
						if (c != '}') return false;
						break;
				}
			}
		}
		return stack.isEmpty();
	}

}
